package com.briup.web;

import com.briup.pojo.Student;
import com.briup.pojo.StudentWithAddress;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@RequestMapping("/path")//类级别的RequestMapping  定义了这个程序类中所有方法的公共请求路径前缀
@RestController//Controller类 必须要添加的 具体后面分解
public class PathController {
    @RequestMapping("/test")
    public String test() {
        System.out.println("in test()");
        return "hello,PathController,test()";
    }

    //    @RequestMapping(path={"/hello","/hi"})//通过path属性指定多个请求路径
    @RequestMapping(value = {"/hello", "/hi"})//通过value属性指定多个请求路径
    public String hello() {
        System.out.println("in hello()");
        return "hello,PathController,hello()";
    }

    //指定请求方法
//    @RequestMapping(value="/method",method= RequestMethod.POST)//通过value属性指定请求路径，method属性指定请求方法
    @RequestMapping(value = "/method", method = {RequestMethod.POST, RequestMethod.GET})
    public String method() {
        System.out.println("in method()");
        return "method,PathController,method()";
    }

    //获取请求参数
    @RequestMapping(value = "3", method = {RequestMethod.POST, RequestMethod.GET})
    public String getParam(HttpServletRequest request) {
        //获取请求参数
        String name = request.getParameter("name");
        String ageStr = request.getParameter("age");
        int age=Integer.parseInt(ageStr);
        System.out.println("name:" + name);
        System.out.println("age:" + age);
        System.out.println("in getParam()");
        return "ok";
    }

    //参数获取
    @RequestMapping("/simpleParam")
    public String simpleParam(String name,Integer age) {
        System.out.println("name:" + name);
        System.out.println("age:" + age);
        return "ok";
    }

    //参数获取- 使用@RequestParam解决请求参数名称不一致无法映射成功的问题
    @RequestMapping("/simpleParam2")
    public String simpleParam2(@RequestParam("usename") String name, Integer age) {
        System.out.println("name:" + name);
        System.out.println("age:" + age);
        return "ok";
    }

    //实体类参数- 请求参数的名称要和Pojo类的属性名称一致 否则无法封装
    @RequestMapping("/simplePojo")
    public String simplePojo(Student stu) {
        System.out.println(stu);
        return "ok";
    }

    //嵌套实体参数- 请求参数的名称要和Pojo类的属性名称一致 否则无法封装
    //http://127.0.0.1:8081/path/complexPojo?name=Lin&age=18&address.province=neimeng&address.city=baotou
    @RequestMapping("/complexPojo")
    public String complexPojo(StudentWithAddress stu) {
        System.out.println(stu);
        return "ok";
    }

    //数组参数- 请求参数的名称要和形参中的数组名称一致 否则无法封装
//  http://127.0.0.1:8081/path/arrayParam?likes=book&likes=music&likes=ball
    //[book, music, ball]
    @RequestMapping("/arrayParam")
        public String arrayParam(String[] likes) {
            System.out.println(Arrays.toString(likes));
            return "ok";
        }

    //集合参数- 请求参数的名称要和形参中的数组名称一致 否则无法封装
//  http://127.0.0.1:8081/path/ListParam?likes=book&likes=music&likes=ball2
    //[book, music, ball2]
    @RequestMapping("/ListParam")
    public String ListParam(@RequestParam List<String> likes) {
        System.out.println(likes);
        return "ok";
    }

    //集合参数- 请求参数的名称要和形参中的数组名称一致 否则无法封装
// http://127.0.0.1:8081/path/jsonParam
   //StudentWithAddress(name=zhnags, age=23, address=Address(province=nmg, city=baotou))
    //必须通过Post请求方式才能发送json书据
    @RequestMapping(value = "/jsonParam", method ={RequestMethod.POST})
    public String jsonParam(@RequestBody StudentWithAddress stu) {
        System.out.println(stu);
        return "ok";
    }

    //根据id删除用户 htt://127.0.0.1:8081/path/deleteUserById?id=1
    //根据id删除用户 htt://127.0.0.1:8081/path/deleteUserById/1
    //路径参数
// http://127.0.0.1:8081/path/deleteUserById/1
    //必须通过Post请求方式才能发送json书据
    @RequestMapping(value = "/deleteUserById/{id}")
    public String jsonParam(@PathVariable Integer id) {
        System.out.println(id);
        return "ok";
    }

    @RequestMapping(value = "/query/{id}/{username}")
    public String query(@PathVariable Integer id, @PathVariable("username") String name) {
        System.out.println(id);
        System.out.println(name);
        return "ok";
    }

    @RequestMapping( "/queryStuById/{id}")
    @ResponseBody//如果返回对象的json格式数据，则需要添加@ResponseBody注解实现
    public Student queryStuById(@PathVariable Integer id) {
        System.out.println(id);
        //返回对象 返回json
        Student student = new Student();
        student.setName("zhnags");
        student.setAge(20);
        Student student1 = new Student();
        student1.setName("zhangsan");
        student1.setAge(21);
        ArrayList<Student> stuList = new ArrayList<>();
        stuList.add(student);
        stuList.add(student1);
        return student1;
    }



}
